package operations;

import aboutTheBook.BookShelf;
import aboutTheBook.Book;
import java.util.Arrays;
import java.util.Scanner;

public class Add implements IOperation, ICheckCapacity {
    @Override
    public boolean checkIsCapacity(BookShelf bookShelf) {
        //当数组的长度等于有效元素个数的时候，需要扩容
        return bookShelf.getBookList().length==bookShelf.getSize();
    }
    @Override
    public void work(BookShelf bookShelf) {
        //上架图书
        //1：判定书架的空间是否足够 ？够--》 继续 ：不够--》扩容
        if(checkIsCapacity(bookShelf)) {
            Book[] bookList = bookShelf.getBookList();
            //二倍扩容
            bookList = Arrays.copyOf(bookList,bookList.length*2);
            bookShelf.setBookList(bookList);
        }
        //2：输入书的信息
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您所上架的书籍类型：");
        String bookType = scanner.nextLine();
        System.out.println("请输入您所上架的书籍名称：");
        String title = scanner.nextLine();
        System.out.println("请输入您所上架的书籍作者：");
        String author = scanner.nextLine();
        System.out.println("请输入该书的存放地点：");
        String location = scanner.nextLine();
        Book book = new Book(bookType,title,author,location);
        //3：放书
        //在size位置放书，size++
        bookShelf.setPosBook(bookShelf.getSize(),book);
        bookShelf.setSize(bookShelf.getSize()+1);
        System.out.println("上架成功！");
    }
}
